package com.android.graffiti;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RadioButton;

import com.common.base.ComponentContext;
import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;
import com.photoeditor.demo.ui.widget.image.edit.DoodleBarView;


/**
 * 表示颜色值的自定义单选按钮
 * 
 * @see RadioButton
 */
public class ColorRadioButton extends RadioButton {

    private static Paint sStrokePaint;
    private static Paint sMaskPaintChecked;
    private Paint mCirclePaint;

    public static final int STROKE_PAINT_WIDTH = DeviceUtils.dip2px(ComponentContext.getContext(), 3);
    
    /**
     * 马赛克图片
     */
    private Paint mMosaicPaint = new Paint();

    private Bitmap mBitmap;

    private boolean mColorCircle;
    
    static {
        if(sStrokePaint == null) {
            sStrokePaint = new Paint();
            sStrokePaint.setColor(ComponentContext.getContext().getResources().getColor(R.color.accent_color));
            sStrokePaint.setStrokeJoin(Paint.Join.ROUND);
            sStrokePaint.setStrokeWidth(STROKE_PAINT_WIDTH);
            sStrokePaint.setStyle(Paint.Style.STROKE);
            sStrokePaint.setAntiAlias(true);
        }
        if(sMaskPaintChecked == null) {
            sMaskPaintChecked = new Paint();
            sMaskPaintChecked.setColor(0x455a64);
            sMaskPaintChecked.setStyle(Paint.Style.FILL);
            sMaskPaintChecked.setAntiAlias(true);
            sMaskPaintChecked.setAlpha(179);
        }
    }
    
    private static Rect sWholeRect;
    private static Rect sWholeRectUnchecked;
    
    private int mColor = 0xFF000000;
    
    /**
     * 类型
     */
    private int mType = 0;
    
    /**
     * 马赛克类型对应的图片
     */
    private int mImage = View.NO_ID;

    private boolean mIsVipRes;
    private Bitmap mVipBitmap;

    /**
     * 获取要显示的颜色值
     * @return 要显示的颜色值
     */
    public int getColor() {
        return mColor;
    }
    
    /**
     * 设置要显示的颜色值
     * @param color 要显示的颜色值
     */
    public void setColor(int color) {
        this.mColor = color;
        invalidate();
    }
    

    public int getType() {
		return mType;
	}

    public int getImage() {
        return mImage;
    }

    public Bitmap getBitmap() {
        return mBitmap;
    }

    public void setBitmap(Bitmap bitmap) {
        mBitmap = bitmap;
        postInvalidate();
    }

    public void setType(int mType) {
		this.mType = mType;
	}

	public ColorRadioButton(Context context) {
        this(context, null);
    }


    public ColorRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initializeWithAttrs(context, attrs);
    }


    public ColorRadioButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initializeWithAttrs(context, attrs);
    }


    /**
     * 使用XML属性进行初始化，<b>初始值请在此处修改</b>
     * @param context 上下文对象
     * @param attrs 属性集
     */
    private void initializeWithAttrs(Context context, AttributeSet attrs) {
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.ColorRadioButton);

        mColor = typedArray.getColor(R.styleable.ColorRadioButton_colorValue,
                0xb3455a64);
        
        mType = typedArray.getInt(R.styleable.ColorRadioButton_colorType, -1);
        mImage = typedArray.getResourceId(R.styleable.ColorRadioButton_colorImage, View.NO_ID);
        mBitmap = BitmapFactory.decodeResource(getResources(), mImage);
        mColorCircle = typedArray.getBoolean(R.styleable.ColorRadioButton_colorCircle, true);
        mCirclePaint = new Paint();
        mCirclePaint.setColor(mColor);
        mCirclePaint.setStyle(Paint.Style.FILL);
        mCirclePaint.setAntiAlias(true);
        
        mMosaicPaint.setStyle(Paint.Style.FILL);
        mMosaicPaint.setAntiAlias(true);
        
        typedArray.recycle();

        mIsVipRes = DoodleBarView.isVipBrushResId(getId());

        mVipBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_vip_small);
    }

    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == VISIBLE) {
            mIsVipRes = DoodleBarView.isVipBrushResId(getId());
        }
    }

    @Override
    public void setChecked(boolean checked) {
        // TODO Auto-generated method stub
        super.setChecked(checked);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(sWholeRect == null) {
            sWholeRect = new Rect(2, 2, getWidth() - 2, getHeight() - 2);
        }
        if(sWholeRectUnchecked == null) {
            sWholeRectUnchecked = new Rect(0, 0, getWidth() - 1, getHeight() - 1);
        }
        
        if (mType == -1) {
        	//圆形颜色
        	canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2-4, mCirclePaint);
            if (mColor == Color.WHITE) {
                sStrokePaint.setColor(0xff262626);
                sStrokePaint.setStrokeWidth(STROKE_PAINT_WIDTH - 1.5f);
                canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - STROKE_PAINT_WIDTH / 2 - 0.5f, sStrokePaint);
                sStrokePaint.setColor(ComponentContext.getContext().getResources().getColor(R.color.accent_color));
                sStrokePaint.setStrokeWidth(STROKE_PAINT_WIDTH);
            }
        } else if (mType == 0) {
            if (mBitmap != null) {
                canvas.drawBitmap(mBitmap, null, new Rect(4, 4, getWidth() - 4, getHeight() - 4), null);
            }
        } else {
            //前面3个马赛克图片
            //圆形图片
            if (mBitmap != null) {
                Shader mShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);
                mMosaicPaint.setShader(mShader);
            }
            canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - 4, mMosaicPaint);
        }
        
        
        if (isChecked() && mColorCircle) {
        	// canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2-4, sMaskPaintChecked);
            canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - ColorRadioButton.STROKE_PAINT_WIDTH / 2 - 0.5f, sStrokePaint);
        }

        if (mIsVipRes) {
            canvas.drawBitmap(mVipBitmap, 0, 0, null);
        }
    }

    public void setVipRes(boolean vipRes) {
        mIsVipRes = vipRes;
    }
}
